package com.kth.subtranslator.driver;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import com.kth.subtranslator.IBatchTranslateCallBack;
import com.kth.subtranslator.ITranslateDriver;
import com.kth.subtranslator.ITranslateDriverConfiguration;
import com.kth.subtranslator.TranslateEngine.Language;

public class WebservicexNetDriver implements ITranslateDriver {

	@Override
	public String translate(String text, Language fromLang, Language toLang) {
		return null;
	}

	@Override
	public Collection<String> batchTranslate(Collection<String> text,
			Language fromLang, Language toLang, IBatchTranslateCallBack callBack) {
		for (String t : text) {
			internalTranslate(t, getLanguageMode(fromLang, toLang));
		}
		return null;
	}

	private void internalTranslate(String text, String languageMode) {
		String resp = doPost("http://www.webservicex.net/translateservice.asmx/Translate", text, languageMode);
		System.out.println("resp = " + resp);
	}

	private String doPost(String urlStr, String text, String languageMode) {
		
		DefaultHttpClient httpclient = new DefaultHttpClient();
		
//		NTCredentials creds = new NTCredentials("xxxx", "xxx", "xxx", "be");
//		httpclient.getCredentialsProvider().setCredentials(AuthScope.ANY, creds);
//		httpclient.getCredentialsProvider().setCredentials(
//                new AuthScope("proxy.ict", 8080),
//                creds);
//		HttpHost proxy = new HttpHost("proxy.ict", 8080);
//		httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
		
		httpclient.getParams().setParameter("http.useragent", "Test Client");
		HttpPost httpPost = new HttpPost("http://posttestserver.com/post.php"/*urlStr*/);
		
		
		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
		nameValuePairs.add(new BasicNameValuePair("LanguageMode", languageMode));
		nameValuePairs.add(new BasicNameValuePair("Text", text));
		
		try {
			httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
		} catch (UnsupportedEncodingException e2) {
			e2.printStackTrace();
		}
		
	    ResponseHandler<String> responseHandler = new BasicResponseHandler();
	    
	    
        try {
			String responseBody = httpclient.execute(httpPost, responseHandler);
			System.out.println("resp : " + responseBody);
		} catch (ClientProtocolException e1) {
			e1.printStackTrace();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
	    
		return "";
	}

	@Override
	public Collection<Language> getSupportedLanguage() {
		return Arrays.asList(Language.FRENCH, Language.ENGLISH);
	}

	@Override
	public String getName() {
		return "Webservicex.net";
	}

	private String getLanguageMode(final Language fromLang, final Language toLang) {
		StringBuilder sb = new StringBuilder();
		if (Language.ENGLISH.equals(fromLang)) {
			sb.append("English");
		}

		sb.append("TO");

		if (Language.FRENCH.equals(toLang)) {
			sb.append("French");
		}
		return sb.toString();
	}

	/* (non-Javadoc)
	 * @see com.kth.subtranslator.ITranslateDriver#stop()
	 */
	@Override
	public void stop() {
		// TODO Auto-generated method stub
		
	}

	/* (non-Javadoc)
	 * @see com.kth.subtranslator.ITranslateDriver#getSupportedTargetLanguage(com.kth.subtranslator.TranslateEngine.Language)
	 */
	@Override
	public Collection<Language> getSupportedTargetLanguage(Language fromLang) {
		return getSupportedLanguage();
	}
	
	/* (non-Javadoc)
	 * @see com.kth.subtranslator.ITranslateDriver#getConfiguration()
	 */
	@Override
	public ITranslateDriverConfiguration getConfiguration() {
		return null;
	}

}
